﻿/**
 * openNode
 * @param name 打开tabitem的funcode（功能注册编码）
 * @param src iframe中的
 * @param funcode 打开页面的pageID
 * @param openmode 节点打开模式,0 默认tab多页签,1 tab单页签,2 弹出窗口
 * @param attrArr 自定义属性,例如{title:'',openTabMax:''}
 */
function openNode(funcode, src, attrArr)
{
	//cleanBillMsg();
	if(src == null) 
		return;
	var title = null;
	if(attrArr != null && attrArr.title != null)
		title = attrArr.title;
	else
		title = funcode;//默认显示的是功能注册编码
	
	//默认以tab多页签打开
	var	openmode = getConfigAttribute("openNodeMode");
 	if((openmode == null) || (openmode == '')){
 		openmode = "0";
 	}  
	switch(openmode){
		//以tab页签的方式打开
		case "0":
			var openTabMax = null;
			if(attrArr != null && attrArr.openTabMax != null)
				openTabMax = attrArr.openTabMax;
			else
			 	openTabMax = 5; //默认最多打开5个标签
			var tab = getComponent("pageTab");
			var items = tab.getTabItems();
			var count = items.length;
			//如果已经打开过，则激活
			for(var i = 0 ;i < count; i++){
				if(items[i].name == funcode){
					tab.activeTab(tab.getItemIndex(items[i]));				
					return;
				}
			}

			if(count > openTabMax){
				continueOpen.tab = tab;
				continueOpen.funcode = funcode;
				continueOpen.title = title;
				continueOpen.src = src;
				ConfirmDialogComp.showDialog(webbx_trans('yer_quickdesk001')+(count-1)+webbx_trans('yer_quickdesk002'), continueOpen, null, null, null);
			} 
			else {
				var item1 = tab.createItem(funcode, title, true);
				tab.activeTab(tab.getItemIndex(item1));
				// 显示"加载图片"
				showLoadingDiv(item1.getObjHtml());
				var div = getNodeFrame(funcode, src);
//					if(div != null)
//						showBillMsg();
				item1.getObjHtml().appendChild(div);
			}
			break;
		//每次打开都覆盖第一个	
		case "1":
			var tab = getComponent("pageTab");
			var items = tab.getTabItems();
			var count = items.length;
			if(count == 2){
				var iframe = items[1].getObjHtml().firstChild.firstChild;
				iframe.src = src;
				items[1].changeTitle(title);
				tab.activeTab(1);
			} else {
				var item1 = tab.createItem(funcode, title, true);
				tab.activeTab(1);
				var div = getNodeFrame(funcode,src);
				item1.getObjHtml().appendChild(div);
				showBillMsg();
			}
			break;
		//以新窗口的方式打开	
		case "2":
			window.open( src, title, "toolbar=no,menubar=no,scrollbar=yes,resizable=yes,location=no,status=no,fullscreen=no");
			showBillMsg();
			break;		
		default:
				break;
	}
};

/**
 * 主页的操作条
 */
WebBxOperateBar.prototype = new BaseComponent;
function WebBxOperateBar(parent, name, left, top, width, height, position, dataset, attrObj)
{
	
	this.base = BaseComponent;
	this.id = name;
	this.base(name, left, top, width, height);
	this.parentOwner = parent;
	this.position = getString(position, "relative");
	this.dataset = dataset;
	// 操作条所在div的当前的状态 normal,max
	this.parentOwner.nowState = "normal";
	
	//搜索条件
	this.where_date = 'month';
	this.where_done_cb = 'false';
	this.where_notdone_cb = 'false';
	this.where_dark_input = null;
	
	// 搜索栏默认显示文字
	this.searchDfValue = "";
	if(attrObj != null)
	{
		if(attrObj.searchDfValue)
			this.searchDfValue = attrObj.searchDfValue;
	}
	this.create();
};

WebBxOperateBar.prototype.create = function()
{
	this.Div_gen = $ce("DIV");
	this.Div_gen.id = this.name + "div";
	this.Div_gen.style.left = this.left;
	this.Div_gen.style.top = this.top;
	this.Div_gen.style.width = this.width;
	this.Div_gen.style.height = this.height;
	this.Div_gen.style.position = this.position;
	if(this.parentOwner)
		this.placeIn(this.parentOwner);
};

WebBxOperateBar.prototype.zoomOut = function() {
	// 点击"我的报销单据"
	if(this.id == "mydjbar")
	{
		if(this.parentOwner.nowState == "normal")
		{
			// 隐藏"我的代办事务"
			$("myjobdiv").nowState = "min";
			$('myjobdiv').parentNode.style.height = 0;
			$('myjobdiv').parentNode.parentNode.style.height = 0;
			$("myjobdiv").style.display = "none";
			// 最大化"我的报销单"
			var container = $('mydjdiv').parentNode;
			container.parentNode.style.height = "100%";
			container.style.height = "100%";
			this.parentOwner.nowState = "max";
			this.zoomImg.src = window.nodeImagePath + '/zoom-out-normal.gif';
		}	
		else if(this.parentOwner.nowState == "max")
		{
			// 显示"我的待办事务"
			$("myjobdiv").nowState = "normal";
			$('myjobdiv').parentNode.style.height = "50%";
			$('myjobdiv').parentNode.parentNode.style.height = "50%";
			$("myjobdiv").style.display = "block";
			// 正常化"我的报销单"
			var container = $('mydjdiv').parentNode;
			container.parentNode.style.height = "49%";
			container.style.height = "49%";
			this.parentOwner.nowState = "normal";
			this.zoomImg.src = window.nodeImagePath + '/zoom-in-normal.gif';
		}
	}
	// 点击"我的待办事务"
	else if(this.id == "myjobbar")
	{
		if(this.parentOwner.nowState == "normal")
		{	
			// 隐藏"我的报销单据"
			$("mydjdiv").nowState = "min";
			$('mydjdiv').parentNode.style.height = 0;
			$('mydjdiv').parentNode.parentNode.style.height = 0;
			$("mydjdiv").style.display = "none";
			
			// 最大化"我的待办事务"
			var container = $('myjobdiv').parentNode;
			container.parentNode.style.height = "100%";
			container.style.height = "100%";
			this.parentOwner.nowState = "max";
			this.zoomImg.src = window.nodeImagePath + '/zoom-out-normal.gif';
		}	
		else if(this.parentOwner.nowState == "max")
		{
			// 显示"我的报销单据"
			$("mydjdiv").nowState = "normal";
			$('mydjdiv').parentNode.style.height = "49%";
			$('mydjdiv').parentNode.parentNode.style.height = "49%";
			$("mydjdiv").style.display = "block";
			// 正常化"我的待办事务"
			var container = $('myjobdiv').parentNode;
			container.parentNode.style.height = "49%";
			this.parentOwner.nowState = "normal";
			this.zoomImg.src = window.nodeImagePath + '/zoom-in-normal.gif';
		}
	}
};

WebBxOperateBar.prototype.manageSelf = function()
{
	var oThis = this;
	this.week = $ce("A");  
	this.week.className = "operator_element";
	oThis.Div_gen.appendChild(this.week);
	this.week.appendChild(document.createTextNode(webbx_trans('yer_quickdesk003')));
	this.week.id = oThis.name + "week"; 
	this.week.onfocus = function()
	{
		this.blur();
	}
	this.week.style.left = 0;
	this.week.href = "#";
	this.week.onclick = function(e) {
		if(!e)
			e = window.event;
		try{
			oThis.resetState(this);
			oThis.where_date = 'week';
			oThis.doCommonQuery();
		}
		catch(error){}
		stopAll(e);
	};

	this.month = $ce("A");  
	this.month.appendChild(document.createTextNode(webbx_trans('yer_quickdesk004'))); 
	this.month.id = oThis.name + "month";
	this.month.className = "operator_element";
	this.month.onfocus = function()
	{
		this.blur();
	}
	this.month.style.left = 50;
	this.month.href = "#";
	this.month.onclick = function(e) {
		if(!e)
			e = window.event;
		try{
			oThis.resetState(this);
			oThis.where_date = 'month';
			oThis.doCommonQuery();
		}
		catch(error){}
		stopAll(e);
	};		
	oThis.Div_gen.appendChild(this.month);
	
	this.month2 = $ce("A");  
	this.month2.id = oThis.name + "month2";
	this.month2.appendChild(document.createTextNode(webbx_trans('yer_quickdesk005'))); 
	this.month2.className = "operator_element";
	this.month2.onfocus = function()
	{
		this.blur();
	}
	this.month2.style.left = 100;
	this.month2.href = "#";
	this.month2.onclick = function(e) {
		if(!e)
			e = window.event;
		try{
			oThis.resetState(this);
			oThis.where_date = 'twomonth';
			oThis.doCommonQuery();
		}
		catch(error){}
		stopAll(e);
	};		
	oThis.Div_gen.appendChild(this.month2);
	
	this.month3 = $ce("A");  
	this.month3.id = oThis.name + "month3";
	this.month3.appendChild(document.createTextNode(webbx_trans('yer_quickdesk006'))); 
	this.month3.className = "operator_element";
	this.month3.onfocus = function()
	{
		this.blur();
	}
	this.month3.style.left = 150;
	this.month3.href = "#";
	this.month3.onclick = function(e) {
		if(!e)
			e = window.event;
		try{
			oThis.resetState(this);
			oThis.where_date = 'threemonth';
			oThis.doCommonQuery();
		}
		catch(error){}
		stopAll(e);
	};				
	oThis.Div_gen.appendChild(this.month3);
	
	//已完成单据
	var done_ml_value = webbx_trans('yer_quickdesk007');
	var done_ml_tip = webbx_trans('yer_quickdesk_tip3');
	if(this.id == 'myjobbar'){
		done_ml_value = webbx_trans('yer_quickdesk_checked');
		done_ml_tip = done_ml_value;
	}
	this.cb = new CheckboxComp(this.Div_gen, oThis.name + "webbx_cb_done",'240', '0','130', done_ml_value,false, 'absolute');
	this.cb.onChange = function(newValue, oldValue){
		oThis.where_done_cb = newValue;
		oThis.doCommonQuery();
	}
	this.cb.input.onfocus = function(){
		this.blur();	
	}
	this.cb.input.onmouseover = function(){
    	oThis.cb.input.title = done_ml_tip;
    }
    
	//未完成单据
	if(this.id == 'myjobbar'){
		var notdone_ml_value = webbx_trans('yer_quickdesk0071');
		var notdone_ml_tip = webbx_trans('yer_quickdesk_tip4');
		if(this.id == 'myjobbar'){
			notdone_ml_value = webbx_trans('yer_quickdesk_unchecked');
			notdone_ml_tip = notdone_ml_value; 
		}
		this.cb3 = new CheckboxComp(this.Div_gen, oThis.name + "webbx_cb_notdone",'360', '0','130', notdone_ml_value,false, 'absolute');
		this.cb3.onChange = function(newValue, oldValue){
			oThis.where_notdone_cb = newValue;
			oThis.doCommonQuery();	
		}
		this.cb3.input.onfocus = function(){
			this.blur();	
		}
		this.cb3.input.onmouseover = function(){
    			oThis.cb3.input.title = notdone_ml_tip;
    		}
		//批量审批按钮--云信--姚晡--20130111
		this.sp = $ce("A");  
		this.sp.appendChild(document.createTextNode(webbx_trans('yer_quickdesk_allsp'))); 
		this.sp.id = oThis.name + "sp";
		this.sp.className = "operator_element";
		this.sp.onfocus = function()
		{
			this.blur();
		}
		this.sp.style.left = 500;
		this.sp.href = "#";
		this.sp.onclick = function(e) {
			if(!e)
				e = window.event;
			try{
				if(!oThis.cb3.checked){
					alert('批量审批时,请勾选未处理的单据');
				}
				else{

					var Rows = oThis.dataset.getSelectedRows();
					if(Rows != null && Rows.length > 0){
						oThis.doQuery2(oThis.dataset.id);
					}else{
						alert('请选择要审批的单据');
					}
				}
			}
		catch(error){}
		stopAll(e);
		};	
		oThis.Div_gen.appendChild(this.sp);
	}
	else{
		var notdone_ml_value = webbx_trans('yer_quickdesk0071');
		var notdone_ml_tip = webbx_trans('yer_quickdesk_tip4');
		if(this.id == 'myjobbar'){
			notdone_ml_value = webbx_trans('yer_quickdesk_unchecked');
			notdone_ml_tip = notdone_ml_value; 
		}
		this.cb2 = new CheckboxComp(this.Div_gen, oThis.name + "webbx_cb_notdone",'380', '0','130', notdone_ml_value,false, 'absolute');
		this.cb2.onChange = function(newValue, oldValue){
			oThis.where_notdone_cb = newValue;
			oThis.doCommonQuery();	
		}
		this.cb2.input.onfocus = function(){
			this.blur();	
		}
		this.cb2.input.onmouseover = function(){
    			oThis.cb2.input.title = notdone_ml_tip;
    		}
	}
	
	var left = oThis.Div_gen.offsetWidth - 240;
	this.searchInput = $ce('input');
	this.searchInput.style.left = left;
	this.Div_gen.appendChild(this.searchInput);
	this.searchInput.style.position = "absolute";
	this.searchInput.style.width = "120px";
	this.searchInput.style.paddingTop = "2px";
	this.searchInput.style.height = "20px";
	this.searchInput.style.border = "1px solid #A0B3C6";
	this.searchInput.style.color = "#777777";
	this.searchInput.value = this.searchDfValue;
	this.searchInput.onclick = function(e)
	{
		oThis.where_dark_input = null;
		this.value = "";
	}
	this.searchInput.onkeydown = function(e)
	{
		if(!e)
			e = window.event;
		var keyCode = e.keyCode ? e.keyCode : e.charCode ? e.charCode : e.which ? e.which : void 0;	
    	if(keyCode == 13)
    	{
    		var qryValue = oThis.searchInput.value;
			oThis.where_dark_input = qryValue;
			oThis.doCommonQuery();
    	}		
	}
	this.searchInput.onmouseover = function(e){
		oThis.searchInput.title = webbx_trans('yer_quickdesk_tip1');
	}
	
	
	this.refImg = $ce("IMG");
    this.refImg.style.position = "absolute";
    this.refImg.style.left = left + 102;
	this.Div_gen.appendChild(this.refImg);
    this.refImg.src = window.nodeImagePath + '/search.gif';
    this.refImg.onclick = function(e)
    {
    	var qryValue = oThis.searchInput.value;
    	oThis.where_dark_input = qryValue;
		oThis.doCommonQuery();
    }
    this.refImg.onmouseover = function(){
    	oThis.refImg.title = webbx_trans('yer_quickdesk_tip1');
    }
    
	this.month4 = $ce("A");  
	this.month4.id = oThis.name + "month4";
	this.Div_gen.appendChild(this.month4);
	this.month4.appendChild(document.createTextNode(webbx_trans('yer_quickdesk008'))); 
	this.month4.style.position = "absolute";
	this.month4.style.left = left + 140;
	this.month4.href = "#";
	this.month4.className = "operator_element";
	this.month4.onfocus = function()
	{
		this.blur();
	}
	this.month4.onclick = function(e) {
		if(!e)
			e = window.event;
		try{
			oThis.doQuery(oThis.dataset.id);
		}
		catch(error){}
		stopAll(e);
	};
	this.month4.onmouseover = function(){
    	oThis.month4.title = webbx_trans('yer_quickdesk_tip2');
    };
	
	this.lineImg = $ce("IMG");
	this.Div_gen.appendChild(this.lineImg);
    this.lineImg.src = window.nodeImagePath + '/line-1.gif';
    this.lineImg.style.position = "absolute";
    this.lineImg.style.left = left + 172;
    this.lineImg.style.top = "3px";
    
   
    this.zoomImg = $ce("IMG");
	this.Div_gen.appendChild(this.zoomImg);
    this.zoomImg.src = window.nodeImagePath + '/zoom-in-normal.gif';
    this.zoomImg.style.position = "absolute";
    this.zoomImg.style.left = left + 181;
    this.zoomImg.style.top = "3px";
    this.zoomImg.onclick = function() {
    	oThis.zoomOut();
    };
     
    this.zoomImg.onmouseover = function() {
    	this.style.cursor = "hand";
    	if(oThis.parentOwner.nowState == "normal")
    		this.src = window.nodeImagePath + '/zoom-in-hover.gif';
    	else
    		this.src = window.nodeImagePath + '/zoom-out-hover.gif';	
    };
    
    this.zoomImg.onmouseout = function() {
    	this.style.cursor = "pointer";
    	if(oThis.parentOwner.nowState == "normal")
    		this.src = window.nodeImagePath + '/zoom-in-normal.gif';
    	else
    		this.src = window.nodeImagePath + '/zoom-out-normal.gif';	
    };
    	
	this.resetState(this.month);
	window["$c_bxbar"] = this;
	this.Div_gen.onresize = function() {
		var left = this.offsetWidth - 200;
		if(left < 420)
			left = 450;
		oThis.month4.style.left = left + 140;
		oThis.searchInput.style.left = left;
	    oThis.refImg.style.left = left + 102;
	    oThis.lineImg.style.left = left + 172;
	    oThis.zoomImg.style.left = left + 181;
	};
	
	this.doCommonQuery('month');
};

WebBxOperateBar.prototype.resetState = function(link) {
	this.week.className = "operator_element";
	this.month.className = "operator_element";
	this.month2.className = "operator_element";
	this.month3.className = "operator_element";
	link.className += " operator_clicked";
};


/***********************************************/
/**
 *常用查询 【单据日期 djrq】
 */
WebBxOperateBar.prototype.doCommonQuery = function()
{	 
	this.$query();
}
	
WebBxOperateBar.prototype.$query = function()
{
	var targetDs = this.dataset;
	targetDs.reqParameterMap.clear();
	
	targetDs.addReqParameter("WEBBX_MAIN_QUERY_DATE" ,this.where_date);
	targetDs.addReqParameter("WEBBX_MAIN_QUERY_DONE", this.where_done_cb);
	targetDs.addReqParameter("WEBBX_MAIN_QUERY_NOTDONE",this.where_notdone_cb);
	targetDs.addReqParameter("WEBBX_MAIN_QUERY_DARK",this.where_dark_input);
	targetDs.addReqParameter(IDatasetConstant.FROM_QUERY_TEMPLATE, "false");
	var args = [targetDs];
	var loader = new DatasetLoader(targetDs);
	loader.setReturnArgs(args);
	loader.setReturnFunc($returnFunc);
	loader.setKeyValue(Dataset.MASTER_KEY);
	loader.load();
};
//XXX 针对ybje的精度
function $returnFunc(args, isSuccess) {
		if(isSuccess){
			var ds = args[0];
			resetSumPrecision(ds);
		}
}

//XXX 重新设置合计行精度，目前只对ybje
function resetSumPrecision(ds){
			if(ds.getRowCount() == 0)
				return ;
			var precision = getMaxPrecision(ds);
			var gridId = (ds.id == 'myDjDs') ? "myDjGrid" : "myJobGrid";
			var g = getComponent(gridId);
			var sum = ds.totalSum(['ybje'],null,null,null,null);
			var sumCells = g.dynSumRowDataDiv.childNodes;
			var header = g.getHeader('ybje');
			header.setPrecision(precision);
			sum = GridComp.parseData(header, sum);
			for(var i = 0, count = sumCells.length; i < count; i++){
				if(sumCells[i].headKey == 'ybje'){ 
					sumCells[i].innerHTML = "<div style='text-align:right'>" + sum + "</div>";
				}	
			}
}

function getMaxPrecision(ds){
	var preIndex = ds.nameToIndex('precision');
	var preArr = []; 
	for(var i=0,count=ds.getRowCount();i<count;i++){
		var row = ds.getRow(i);
		var precision = parseInt(row.getCellValue(preIndex));
		preArr.push(precision);
	}
	preArr = preArr.sort(intSortFunc2);
	return preArr[0];
}
function intSortFunc2(a ,b){
	return b-a;	
}

WebBxOperateBar.prototype.getMainFrame = function() {
	return parent.mainFrame;
}

/**
 *高级查询
 */
WebBxOperateBar.prototype.doQuery = function(dsId)
{  
	var url = window.corePath + "/quickquery/quickquery.jsp?pageId=quickquery&dsId="+dsId+"&rand=" + (Math.random()*10000).toString().substring(0, 4);
	var dlgid = dsId +"_quickquery_dialog"; 
	showDialog(url , webbx_trans('yer_query'), "600", "240", dlgid, false, false);
}
/**
 *批量审批
 */
WebBxOperateBar.prototype.doQuery2 = function(dsId)
{ 
	var url = window.corePath + "/spquery.jsp?pageId=webbxmainsp&dsId="+dsId+"&rand=" + (Math.random()*10000).toString().substring(0, 4);
	showDialog(url , "批量审批", "300", "150", "sp_dialog", false, true);
}